#pragma once
#include "ResourcePool.h"
#include "ThreadPool.h"
#include <unordered_map>
#include <memory>
#include <string>

namespace common_component {

/**
 * @brief 通用资源管理器 - 统一管理线程池和对象池
 */
class ResourceManager {
public:
    // 单例模式
    static ResourceManager& Instance();
    
    // 禁止拷贝和赋值
    ResourceManager(const ResourceManager&) = delete;
    ResourceManager& operator=(const ResourceManager&) = delete;
    
    ~ResourceManager();
    
    // 线程池管理
    std::shared_ptr<ThreadPool> GetThreadPool(const std::string& name = "default");
    std::shared_ptr<ThreadPool> CreateThreadPool(
        const std::string& name, 
        size_t num_threads = 0
    );
    void RemoveThreadPool(const std::string& name);
    
    // 对象池管理
    template<typename T>
    std::shared_ptr<ObjectPool<T>> GetObjectPool(const std::string& name = "default");
    
    template<typename T>
    std::shared_ptr<ObjectPool<T>> CreateObjectPool(
        const std::string& name,
        size_t initial_size = 10,
        size_t max_size = 100,
        size_t high_water_mark = 5
    );
    
    template<typename T>
    void RemoveObjectPool(const std::string& name);
    
    // 统计信息
    struct Statistics {
        size_t thread_pool_count;
        size_t object_pool_count;
        size_t total_threads;
        size_t total_objects;
    };
    
    Statistics GetStatistics() const;
    void PrintStatistics() const;
    
    // 资源清理
    void Shutdown();
    void Clear();
    
private:
    ResourceManager() = default;
    
    mutable std::mutex mutex_;
    std::unordered_map<std::string, std::shared_ptr<ThreadPool>> thread_pools_;
    
    // 使用type_info作为key来存储不同类型的对象池
    std::unordered_map<std::string, std::shared_ptr<void>> object_pools_;
    
    bool shutdown_ = false;
};

// 模板方法实现
template<typename T>
std::shared_ptr<ObjectPool<T>> ResourceManager::GetObjectPool(const std::string& name) {
    std::lock_guard<std::mutex> lock(mutex_);
    
    std::string key = std::string(typeid(T).name()) + "_" + name;
    auto it = object_pools_.find(key);
    
    if (it != object_pools_.end()) {
        return std::static_pointer_cast<ObjectPool<T>>(it->second);
    }
    
    // 如果不存在,创建默认的
    return CreateObjectPool<T>(name);
}

template<typename T>
std::shared_ptr<ObjectPool<T>> ResourceManager::CreateObjectPool(
    const std::string& name,
    size_t initial_size,
    size_t max_size,
    size_t high_water_mark
) {
    std::lock_guard<std::mutex> lock(mutex_);
    
    if (shutdown_) {
        return nullptr;
    }
    
    std::string key = std::string(typeid(T).name()) + "_" + name;
    
    auto pool = std::make_shared<ObjectPool<T>>(initial_size, max_size, high_water_mark);
    object_pools_[key] = pool;
    
    return pool;
}

template<typename T>
void ResourceManager::RemoveObjectPool(const std::string& name) {
    std::lock_guard<std::mutex> lock(mutex_);
    
    std::string key = std::string(typeid(T).name()) + "_" + name;
    auto it = object_pools_.find(key);
    
    if (it != object_pools_.end()) {
        // 先关闭池
        auto pool = std::static_pointer_cast<ObjectPool<T>>(it->second);
        pool->shutdown();
        
        // 然后移除
        object_pools_.erase(it);
    }
}

}  // namespace common_component
